<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品型号" prop="productModel">
        <el-input
          v-model="queryParams.productModel"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="故障率" prop="failureRate">
        <el-input
          v-model="queryParams.failureRate"
          placeholder="请输入故障率"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="维护信息" prop="maintenanceInfo">
        <el-input
          v-model="queryParams.maintenanceInfo"
          placeholder="请输入维护信息"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所用项目" prop="projectUsed">
        <el-input
          v-model="queryParams.projectUsed"
          placeholder="请输入所用项目"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所在电站" prop="powerStation">
        <el-input
          v-model="queryParams.powerStation"
          placeholder="请输入所在电站"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所用机组" prop="unitUsed">
        <el-input
          v-model="queryParams.unitUsed"
          placeholder="请输入所用机组"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所用数量" prop="quantityUsed">
        <el-input
          v-model="queryParams.quantityUsed"
          placeholder="请输入所用数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="equipmentCode">
        <el-input
          v-model="queryParams.equipmentCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['om:product-self-health:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['om:product-self-health:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品型号" align="center" prop="productModel" />
      <el-table-column label="故障率" align="center" prop="failureRate" />
      <el-table-column label="维护信息" align="center" prop="maintenanceInfo" />
      <el-table-column label="所用项目" align="center" prop="projectUsed" />
      <el-table-column label="所在电站" align="center" prop="powerStation" />
      <el-table-column label="所用机组" align="center" prop="unitUsed" />
      <el-table-column label="所用数量" align="center" prop="quantityUsed" />
      <el-table-column label="设备编码" align="center" prop="equipmentCode" />
      <el-table-column label="卡件条形码" align="center" prop="cardBarcode" />
      <el-table-column label="PCB版本" align="center" prop="pcbVersion" />
      <el-table-column label="是否返修" align="center" prop="isRepaired" />
      <el-table-column label="关联ERP售后派工单编号" align="center" prop="erpWorkorderNo" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['om:product-self-health:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['om:product-self-health:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProductSelfHealthForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProductSelfHealthApi, ProductSelfHealthVO } from '@/api/om/productselfhealth'
import ProductSelfHealthForm from './ProductSelfHealthForm.vue'

/** 已供货自研类产品健康管理档案 列表 */
defineOptions({ name: 'ProductSelfHealth' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProductSelfHealthVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  productName: undefined,
  productModel: undefined,
  failureRate: undefined,
  maintenanceInfo: undefined,
  projectUsed: undefined,
  powerStation: undefined,
  unitUsed: undefined,
  quantityUsed: undefined,
  equipmentCode: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProductSelfHealthApi.getProductSelfHealthPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductSelfHealthApi.deleteProductSelfHealth(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductSelfHealthApi.exportProductSelfHealth(queryParams)
    download.excel(data, '已供货自研类产品健康管理档案.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>